package com.autonavi.yunda.yunji.core.model.condition;

import com.autonavi.yunda.yunji.core.enums.PartUnitInterfaceStatus;
import com.autonavi.yunda.yunji.core.model.dto.PartUnitDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

@Data
public class PartUnitQueryCondition {

    private String type;

    private String version;

    private String title;

    private String falconId;

    private String createUser;
    /**
     * 最小值是1
     */
    public int pageNo;

    public int pageSize;
    private PartUnitInterfaceStatus status;

    public QueryWrapper<PartUnitDTO> getQueryWrapper() {
        QueryWrapper<PartUnitDTO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        if (StringUtils.isNotBlank(this.getType())) {
            wrapper.eq("type", this.getType());
        }
        if (Strings.isNotBlank(this.getVersion())) {
            wrapper.eq("version", this.getVersion());
        }
        if (StringUtils.isNotBlank(this.getFalconId())) {
            wrapper.eq("falcon_id", this.getFalconId());
        }
        if (this.getStatus() != null) {
            wrapper.eq("status", this.getStatus());
        }
        if (StringUtils.isNotBlank(this.getTitle())) {
            wrapper.like("title", String.join("%", this.getTitle(), "%"));
        }
        if (StringUtils.isNotBlank(this.getCreateUser())) {
            wrapper.like("create_user", String.join("%", this.getCreateUser(), "%"));
        }
        return wrapper;
    }

    public boolean validate() {
        return this.pageNo > 0 && this.pageSize > 0;
    }
}
